package com.rd.student.ruanjian.class3.gaochenxi.LianXi3.NumberTwo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//用callable接口来实现
public class AddHeThird {
   public static void main(String[] args) throws ExecutionException, InterruptedException {
      int tmp = 0;
      for (int i = 0; i < 10; i++) {
         Test1 test1 = new Test1(i * 10 + 1, i * 10 + 10);
         FutureTask<Integer> futureTask = new FutureTask(test1);
         new Thread(futureTask, "Part" + (i + 1) + " ").start();
         futureTask.get();//get()方法是个阻塞作用，有了它就可以进行排序
      }
      System.out.println("所有数相加的总和为:" + Test1.zong);
   }
}

class Test1 implements Callable<Integer> {
   int start;
   int end;
   int sum = 0;//每一段是每一段的和，不需要叠加，需要变，所以是变量
   static int zong = 0;//是每一段叠加的和，是在上一个基础之上进行加和，所以说是静态变量

   public Test1(int start, int end) {
      this.start = start;
      this.end = end;
   }

   @Override
   public Integer call() throws Exception {
      for (int i = start; i <= end; i++) {
         sum += i;
      }
      zong += sum;
      System.out.println(Thread.currentThread().getName() + "  " + sum);
      return sum;
      //zong是总和
      //sum是分段和
   }
}

